﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;

namespace FormattingLib
{

    public static class NumberFormatting
    {


        /// <summary>
        ///                        a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8
        /// =====================  ======  ======  =====  =====  =====
        /// Floor                    -3      -1      0      1      2
        /// Ceiling                  -2       0      1      2      3
        /// Truncate                 -2       0      0      1      2
        /// Round (ToEven)           -3       0      0      2      3
        /// Round (AwayFromZero)     -3      -1      0      2      3
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="roundingType"></param>
        /// <returns></returns>
        public static double GetRoundedNumber(double number, RoundingType roundingType, int decimals)
        {
            double result = number;
            switch (roundingType)
            {
                case RoundingType.Floor:
                    result = Math.Floor(number);
                    break;
                case RoundingType.Ceiling:
                    result = Math.Ceiling(number);
                    break;
                case RoundingType.Round:
                    result = Math.Round(number, decimals, MidpointRounding.AwayFromZero);
                    break;
                case RoundingType.None:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="roundingType"></param>
        /// <returns></returns>
        public static double GetRoundedNumber(double number, RoundingType roundingType)
        {
            return GetRoundedNumber(number, roundingType, 3);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static double GetRoundedNumber(double number)
        {
            return GetRoundedNumber(number, RoundingType.Round, 3);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="format"></param>
        /// <param name="zeroResultString"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        //public static string GetFormattedNumber(double number, string format, CultureInfo culture, string zeroResultString)
        public static string GetFormattedNumber(double number, string zeroResultString, CultureInfo culture, string format)
        {
            if (number < 0D)
            {
                throw new Exception("Unable to format the number \"0\"!");
            }

            if (number == 0D)
            {
                return zeroResultString;
            }

            string formattedNumber;
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            try
            {
                if (culture != null && culture != currentCulture)
                {
                    Thread.CurrentThread.CurrentCulture = culture;
                }
                formattedNumber = String.Format(format, number);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
            }

            return formattedNumber;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="format"></param>
        /// <param name="zeroResultString"></param>
        /// <returns></returns>
        public static string GetFormattedNumber(double number, string zeroResultString, CultureInfo culture)
        {
            return GetFormattedNumber(number, zeroResultString, culture, "{0:0.000}");
        }
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string GetFormattedNumber(double number, string zeroResultString)
        {
            return GetFormattedNumber(number, zeroResultString, new CultureInfo("de-DE"), "{0:0.000}");
        }
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string GetFormattedNumber(double number)
        {
            return GetFormattedNumber(number,  "0",  new CultureInfo("de-DE"), "{0:0.000}");
        }

        

    }
}
